#include "enemy_3.h"

extern int G_W,G_H;

ENEMY_3::ENEMY_3(SCENE *scene, QTimeLine *global_tl, CASH *cash):QGraphicsObject()
{

    go = new SOUND_POWER(cash,"Res/Sound/Enemy/Enemy_3/go.ogg",this);
    go->Play_Now();

    stvol=QPoint(92,148);

    qsrand(QTime::currentTime().second());

    respaun_point_1 = QPoint(G_W/2,0);

    go_to_left=0+qrand()%(1-0+1);

    point=5000;

    border = cash->cash_poly->value("Res/Ships/Enemy/Enemy_3/Enemy_3.poly");
    im = cash->cash_pixmap->value("Res/Ships/Enemy/Enemy_3/Enemy_3.png");

    this->setZValue(0.5);

    this->setPos( G_W/2,im->height()*(-1) );     // Начальная позиция

    anim_enemy = new QGraphicsItemAnimation();

    enemy_time_line = new QTimeLine(20000);                                             // Скорость перемещения обьекта по траектории
    enemy_time_line->setEasingCurve( QEasingCurve::Type(25));

    enemy_time_line->setFrameRange(0, 1);
    anim_enemy->setTimeLine(enemy_time_line);
    anim_enemy->setItem(this);

    anim_enemy->setPosAt(0,QPointF(G_W/2,(im->height())*(-1) ));

    if(go_to_left){
        anim_enemy->setPosAt(0.1,QPointF(G_W/2, -100) );
        anim_enemy->setPosAt(0.2,QPointF( 100, -100 ));
        anim_enemy->setPosAt(0.3,QPointF(G_W/2, -100));
        anim_enemy->setPosAt(0.4,QPointF( 100, -100 ));
        anim_enemy->setPosAt(0.5,QPointF(100,(im->height())*(-1) ));
    }
    else{
        anim_enemy->setPosAt(0.1,QPointF(G_W/2, -100) );
        anim_enemy->setPosAt(0.2,QPointF(G_W-270, -100 ));
        anim_enemy->setPosAt(0.3,QPointF(G_W/2, -100));
        anim_enemy->setPosAt(0.4,QPointF(G_W-270, -100 ));
        anim_enemy->setPosAt(0.5,QPointF(G_W-270,(im->height())*(-1) ));
    }

    enemy_time_line->start();

    collisia_control = new COLLISIA_CONTROL(scene,((QGraphicsObject*)this),this,global_tl,cash,10);

    connect(this,SIGNAL(destroyed()),SLOT(Emit_Kill_My_Laser()));
    connect(enemy_time_line,SIGNAL(finished()),SLOT(deleteLater()));
    connect(enemy_time_line,SIGNAL(valueChanged(qreal)),SLOT(Waiting_To_Fire(qreal)));

}

QPainterPath ENEMY_3::shape() const
{

    return *border;

}

QRectF ENEMY_3::boundingRect() const
{

    return QRectF( QPointF(0,0),im->size() );

}

void ENEMY_3::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{

    painter->save();
    painter->drawPixmap( 0,0,*im );
    painter->restore();

}

ENEMY_3::~ENEMY_3()
{
    go->Stop();
}

int ENEMY_3::type() const
{

    return point;

}

void ENEMY_3::Waiting_To_Fire(qreal r)
{

    if(r>=0.100000)
        emit Fire(QPoint(this->pos().x()+stvol.x(),this->pos().y()+stvol.y()));

    if(r>=0.400000)
        emit Kill_My_Laser();

}

void ENEMY_3::Emit_Kill_My_Laser()
{

    emit Kill_My_Laser();

}


